package com.ruoyi.project.rest.member.service.impl;

import java.util.Date;
import java.util.List;
import java.util.jar.JarEntry;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.Seq;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.rest.trade.domain.RestMemberTrade;
import com.ruoyi.project.rest.trade.mapper.RestMemberTradeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.rest.member.mapper.RestMemberMapper;
import com.ruoyi.project.rest.member.domain.RestMember;
import com.ruoyi.project.rest.member.service.IRestMemberService;
import com.ruoyi.common.utils.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 会员Service业务层处理
 *
 * @author ruoyi
 * @date 2022-03-28
 */
@Service
public class RestMemberServiceImpl implements IRestMemberService {
    @Autowired
    private RestMemberMapper restMemberMapper;
    @Autowired
    private RestMemberTradeMapper restMemberTradeMapper;

    /**
     * 查询会员
     *
     * @param id 会员主键
     * @return 会员
     */
    @Override
    public RestMember selectRestMemberById(Long id) {
        return restMemberMapper.selectRestMemberById(id);
    }

    /**
     * 查询会员列表
     *
     * @param restMember 会员
     * @return 会员
     */
    @Override
    public List<RestMember> selectRestMemberList(RestMember restMember) {
        return restMemberMapper.selectRestMemberList(restMember);
    }

    /**
     * 新增会员
     *
     * @param restMember 会员
     * @return 结果
     */
    @Override
    public int insertRestMember(RestMember restMember) {
        restMember.setPhone(restMember.getPhone().replace("-",""));
        restMember.setMemberNo(Seq.getId());
        restMember.setCreateTime(DateUtils.getNowDate());
        return restMemberMapper.insertRestMember(restMember);
    }

    /**
     * 修改会员
     *
     * @param restMember 会员
     * @return 结果
     */
    @Override
    public int updateRestMember(RestMember restMember) {
        return restMemberMapper.updateRestMember(restMember);
    }

    /**
     * 批量删除会员
     *
     * @param ids 需要删除的会员主键
     * @return 结果
     */
    @Override
    public int deleteRestMemberByIds(String ids) {
        return restMemberMapper.deleteRestMemberByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除会员信息
     *
     * @param id 会员主键
     * @return 结果
     */
    @Override
    public int deleteRestMemberById(Long id) {
        return restMemberMapper.deleteRestMemberById(id);
    }

    @Override
    @Transactional
    public AjaxResult recharge(RestMember restMember) {
        RestMember member = restMemberMapper.selectRestMemberById(restMember.getId());
        if (member == null) {
            return AjaxResult.error("用户不存在");
        }

        RestMemberTrade trade = new RestMemberTrade();
        trade.setType(101);
        trade.setMemberNo(member.getMemberNo());
        trade.setBeforeMoney(member.getBalance());
        trade.setMoney(restMember.getBalance());
        trade.setAfterMoney(restMember.getBalance().add(member.getBalance()));
        trade.setCreateTime(new Date());

        restMember.setBalance(restMember.getBalance().add(member.getBalance()));
        restMemberMapper.updateRestMember(restMember);

        //添加充值记录
        restMemberTradeMapper.insertRestMemberTrade(trade);
        return AjaxResult.success("充值成功");
    }

    @Override
    public AjaxResult checkMember(RestMember restMember) {
        if (StringUtils.isEmpty(restMember.getPhone())) {
            return AjaxResult.error("电话号码不能为空");
        }
        RestMember member = restMemberMapper.checkMember(restMember);
        if (member != null) {
            return AjaxResult.success();
        }
        return AjaxResult.error("会员不存在");
    }

    @Override
    public RestMember selectRestMemberByPhone(String phone) {
        return restMemberMapper.selectRestMemberByPhone(phone);
    }
}
